package com.yankaizhang.blog.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yankaizhang.blog.entity.Article;
import com.yankaizhang.blog.service.ArticleService;
import com.yankaizhang.blog.utils.CommonUtils;
import com.yankaizhang.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yankai
 * @since 2020-10-02
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController {

    private static final int PAGE_SIZE = 20;

    @Autowired
    ArticleService articleService;

    /**
     * 编辑文章
     */
    @RequestMapping("/modify/{aid}")
    public String getSingleById(@PathVariable("aid") Integer aid, Model model){
        try {
            Article record = articleService.getOne(new QueryWrapper<Article>().eq("id", aid));
            model.addAttribute("record", record);
            return "admin/editor";
        }catch (Exception e){
            return "admin/article";
        }
    }


    /**
     * 更新文章
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result updateArticle(
            @RequestParam(value = "aid", required = true) int aid,
            @RequestParam(value = "title", required = true) String title,
            @RequestParam(value = "content", required = true) String content,
            @RequestParam(value = "tags", defaultValue = "", required = false) String tags,
            @RequestParam(value = "status", defaultValue = "1", required = false) int status,
            @RequestParam(value = "type", defaultValue = "0", required = false) int type,
            Model model){
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        int countWords = CommonUtils.countWords(content);
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setTags(tags);
        article.setStatus(status);
        article.setType(type);
        article.setWordCount(countWords);
        article.setReadTime(countWords / 500 + 1);
        updateWrapper.eq("id", aid);
        boolean result = articleService.update(article, updateWrapper);
        if (result){
            return Result.buildSuccess("编辑文章成功");
        }
        return Result.buildError("编辑文章失败");
    }

    /**
     * 删除文章
     */
    @RequestMapping("/delete/{aid}")
    @ResponseBody
    public Result deleteArticle(@PathVariable("aid") Integer aid){
        boolean result = articleService.removeById(aid);
        if (result){
            return Result.buildSuccess("删除文章成功");
        }
        return Result.buildError("删除文章失败");
    }

    /**
     * 发布文章
     */
    @RequestMapping("/publish")
    @ResponseBody
    public Result addArticle(
            @RequestParam(value = "title") String title,
            @RequestParam(value = "content") String content,
            @RequestParam(value = "tags", defaultValue = "", required = false) String tags,
            @RequestParam(value = "status", defaultValue = "1", required = false) int status,
            @RequestParam(value = "type", defaultValue = "0", required = false) int type
    ){
        Article article = new Article();
        int countWords = CommonUtils.countWords(content);
        article.setTitle(title);
        article.setContent(content);
        article.setTags(tags);
        article.setStatus(status);
        article.setType(type);
        article.setWordCount(countWords);
        article.setReadTime(countWords / 500 + 1);
        article.setViews(0);
        article.setCreateTime(System.currentTimeMillis()/1000L);
        try {
            articleService.save(article);
            return Result.buildSuccess("文章发布成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.buildError("发布文章失败");
    }
}
